﻿using Transformable_Engine_v2.Engine.Animation.ColorAnimation;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Global;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Rendering;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Transformable_Engine_v2.Engine.GameObjects.Primitives
{
    public class RectanglePrimitive : Container, IColorAnimatable
    {
        public override TE_Vector2 AbsolutePosition
        {
            protected set
            {
                base.AbsolutePosition = value;
                if(_rectangleShape != null)
                    _rectangleShape.Position = AbsolutePosition.ToVector2f();
            }
        }

        public TE_Rectangle Shape
        {
            set
            {
                Position = value.Position.ToGridbound();
                _rectangleShape.Size = value.Size.ToGridbound().ToVector2f();
            }   
        }

        private Color _fillColor;
        public Color FillColor
        {
            set
            {
                _fillColor = value;
                _rectangleShape.FillColor = _fillColor.ToSFMLColor();
            }
        }

        public Color OutlineColor
        {
            set
            {
                _rectangleShape.OutlineColor = value.ToSFMLColor();
            }
        }

        public int OutlineThickness
        {
            set
            {
                _rectangleShape.OutlineThickness = value;
            }
        }

        private readonly SFML.Graphics.RectangleShape _rectangleShape;

        public RectanglePrimitive()
        {
            _rectangleShape = new  SFML.Graphics.RectangleShape();
        }

        public override void Draw()
        {
            if(Visible)
            {
                if (ShaderHandler.HasShader)
                    RenderTargetHandler.GetCurrentRenderTarget().Draw(_rectangleShape, ShaderHandler.CurrentShader);
                else
                    RenderTargetHandler.GetCurrentRenderTarget().Draw(_rectangleShape);
            }            
        }

        protected override bool SetAnimationAffectedObject(Animation.BaseAnimation animation)
        {
            if (animation is ColorAnimation)
            {
                (animation as ColorAnimation).AffectedObject = this;
                return true;
            }

            return base.SetAnimationAffectedObject(animation);
        }

        public Color GetColor()
        {
            return _fillColor;
        }

        public void AddToColor(Color color)
        {
            FillColor = _fillColor + color;
        }
    }
}